package com.feather.common.web;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItem;

/**
 * @author flogyin
 */
public class SimpleXFileItem implements XFileItem {
	protected FileItem	fileItem;
	protected String	newName;

	public SimpleXFileItem(FileItem fileItem) {
		this.fileItem = fileItem;
	}

	@Override
	public String getFieldName() {
		return this.fileItem.getFieldName();
	}

	@Override
	public String getContentType() {
		return this.fileItem.getContentType();
	}

	@Override
	public String getName() {
		return newName == null ? this.fileItem.getName() : newName;
	}

	@Override
	public void setName(String name) {
		this.newName = name;
	}

	@Override
	public long getSize() {
		return this.fileItem.getSize();
	}

	@Override
	public byte[] get() {
		return this.fileItem.get();
	}

	@Override
	public File getStoreLocation() {
		return this.fileItem instanceof DiskFileItem ? ((DiskFileItem) this.fileItem).getStoreLocation() : null;
	}

	@Override
	public InputStream getInputStream() throws IOException {
		return this.fileItem.getInputStream();
	}

	@Override
	public OutputStream getOutputStream() throws IOException {
		return this.fileItem.getOutputStream();
	}

	@Override
	public void write(File file) throws Exception {
		this.fileItem.write(file);
	}

	@Override
	public void delete() {
		this.fileItem.delete();
	}
}
